package com.matrix.rabbitmqtest.publishandconfirm;

import com.matrix.rabbitmqtest.util.AppKeys;
import com.matrix.rabbitmqtest.util.RabbitMQUtil;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DefaultConsumer;
import com.rabbitmq.client.Envelope;

import java.io.IOException;

public class Consumer {
    public static void main(String[] args) throws IOException {
        Channel channel = RabbitMQUtil.getChannel();
        channel.queueDeclare(AppKeys.QUEUE_NAME, true, false, true, null);
        System.out.println("Receiving Messages...");
        channel.basicConsume(AppKeys.QUEUE_NAME, false, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("Receive: " + new String(body));
                getChannel().basicAck(envelope.getDeliveryTag(), false);
            }
        });
    }
}
